<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>收货方弹出层</title>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <style type="text/css">
        body{
            text-align:center;
        }
    </style>
</head>
<body>
    <form class="layui-form" lay-filter="updateConsi">
        <table class="layui-table">
            <tr>
                <td>收货人:</td>
                <td><input type="text" required lay-verify="required" name="consiPerson" class="layui-input"></td>
                <td>收货单位:</td>
                <td><input type="text" required lay-verify="required" name="consiCom" class="layui-input"></td>
                <td>联系电话:</td>
                <td><input type="text" required lay-verify="required" name="consiTelephone" class="layui-input"></td>
            </tr>
            <tr>
                <td>地址:</td>
                <td><input type="text" required lay-verify="required" name="consiAddress" class="layui-input"></td>
                <td>详细地址:</td>
                <td><input type="text" required lay-verify="required" name="consiDetailAddress" class="layui-input"></td>
                <td>客户等级:</td>
                <td>
                    <select class="layui-select" required lay-verify="required" name="consiGrade">
                        <option value=""></option>
                        <option value="1">优质</option>
                        <option value="2">良好</option>
                        <option value="3" >一般</option>
                        <option value="4">糟糕</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>银行账户:</td>
                <td>
                    <input type="text" required lay-verify="required" name="consiBankcard" class="layui-input">
                    <input type="text" required lay-verify="required" name="consiBankcardNum" class="layui-input">
                </td>
                <td>评价备注:</td>
                <td><textarea placeholder="请输入内容" class="layui-textarea" name="consiRemark"></textarea></td>
                <td>开单员:</td>
                <td>
                    <input type="text" name="empName" disabled="disabled" class="layui-input" >
                    <input type="hidden" name="consiSubmitter" disabled="disabled" class="layui-input" >
                    <input type="hidden" name="consiId">
                </td>
            </tr>
        </table>
        <button class="layui-btn" lay-submit lay-filter="subConsi">确定</button>
    </form>
</body>
<script src="../layui/layui.js"></script>
<script>
    layui.use(['form','jquery','layer'],function () {
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;

        var event = window.sessionStorage.getItem("event");
        var consiId = window.sessionStorage.getItem("consiId");

        /**
         * 加载开单员
         */
        $.ajax({
            type: "POST",
            url: "/getLoginedEmp",
            success: function (data) {
                console.log(data);
                $("input[name='empName']").val(data['empName']);
                $("input[name='consiSubmitter']").val(data['empAccount']);
            }
        });

        if(event === 'add'){//判断是否新增
            form.on('submit(subConsi)', function(data){//参数为按钮lay-filter
                console.log(data.field)
                $.ajax({
                    type: "POST",
                    url: "/insertConsi",
                    dataType:"json",
                    data: data.field,
                    success: function (data) {
                        layer.msg(data['msg'], {icon: 6, time: 1400});
                        if(data['msg'] == '新增成功' ){
                            //再执行关闭
                            parent.layer.closeAll();
                            //修改成功后刷新父界面
                            parent.layui.table.reload('consiTable');
                        }

                    }
                });
                return false;
            });
        }else if(event === 'edit'){//判断是否修改
            $.ajax({
                type:'post'
                ,url:'/byIdQueryConsi'
                ,data:{consiId:consiId}
                ,success:function (data) {
                    form.val("updateConsi",{//参数为表单lay-filter值
                        consiPerson: data['consiPerson'],//收货人
                        consiCom: data['consiCom'],//收货单位
                        consiTelephone: data['consiTelephone'],//联系电话
                        consiAddress: data['consiAddress'],//地址
                        consiDetailAddress: data['consiDetailAddress'],//详细地址
                        consiGrade: data['consiGrade'],//客户等级
                        consiBankcard: data['consiBankcard'],//银行户
                        consiBankcardNum: data['consiBankcardNum'],//卡号
                        consiRemark: data['consiRemark'],//评价备注
                        consiSubmitter: data['consiSubmitter'],//开单员
                        consiId: data['consiId']//收货方id
                    });
                }
            });
            //表单的监听提交
            form.on('submit(subConsi)', function(data){//需要按钮lay-filter值
                $.ajax({
                    type: "post",
                    url: "/updateConsi",
                    dataType:"json",
                    data: data.field,
                    success: function (data) {
                        layer.msg(data['msg'], {icon: 6, time: 1400});
                        if(data['msg'] == '修改成功' ){
                            //再执行关闭
                            parent.layer.closeAll();
                            //修改成功后刷新父界面
                            parent.layui.table.reload('consiTable');//参数为数据表格id
                        }
                    }
                });
                return false;
            });
        }
        form.render();//只有执行了这一步，部分表单元素才会自动修饰成功
    })
</script>
</html>